/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.usc.cs576.lib.mediaObject;

import edu.usc.cs576.lib.Action;
import edu.usc.cs576.lib.MediaObject;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * basic object for image
 * @author haoxu
 */
public class Image implements MediaObject {

    String filename;
    String filepath;
    int width;
    int height;
    java.awt.Image image;
    int x;
    int y;
    int resize_x;
    int resize_y;
    ArrayList<Action> actions;
    int startFrame;
    int endFrame;

    public Image() {
        this.startFrame = -1;
        this.endFrame = -1;
    }

    public void load() {
        InputStream is = null;
        try {
            if (filename == null || filepath == null || width < 0 | height < 0) {
                System.err.println("values should be specified first");
                return;
            }
            File imageFile = new File(filepath);
            is = new FileInputStream(imageFile);
            image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
            long len = imageFile.length();
            byte[] bytes = new byte[(int) len];
            int offset = 0;
            int numRead = 0;
            while (offset < bytes.length && (numRead = is.read(bytes, offset, bytes.length - offset)) >= 0) {
                offset += numRead;
            }
            int ind = 0;
            for (int yy = 0; yy < height; yy++) {
                for (int xx = 0; xx < width; xx++) {
                    byte a = 0;
                    byte r = bytes[ind];
                    byte g = bytes[ind + height * width];
                    byte b = bytes[ind + height * width * 2];
                    int pix = 0xff000000 | ((r & 0xff) << 16) | ((g & 0xff) << 8) | (b & 0xff);
                    ((BufferedImage) image).setRGB(xx, yy, pix);
                    ind++;
                }
            }
        } catch (IOException ex) {
            Logger.getLogger(Image.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            try {
                is.close();
            } catch (IOException ex) {
                Logger.getLogger(Image.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

    public void show() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public String getFilename() {
        return filename;
    }

    public void setFilename(String filename) {
        this.filename = filename;
    }

    public String getFilepath() {
        return filepath;
    }

    public void setFilepath(String filepath) {
        this.filepath = filepath;
    }

    public int getHeight() {
        return height;
    }

    public void setHeight(int height) {
        this.height = height;
    }

    public java.awt.Image getImage() {
        return image;
    }

    public void setImage(java.awt.Image image) {
        this.image = image;
    }

    public int getWidth() {
        return width;
    }

    public void setWidth(int width) {
        this.width = width;
    }

    public int getX() {
        return x;
    }

    public void setX(int x) {
        this.x = x;
    }

    public int getY() {
        return y;
    }

    public void setY(int y) {
        this.y = y;
    }

    public int getResize_x() {
        return resize_x;
    }

    public void setResize_x(int resize_x) {
        this.resize_x = resize_x;
    }

    public int getResize_y() {
        return resize_y;
    }

    public void setResize_y(int resize_y) {
        this.resize_y = resize_y;
    }

    public ArrayList<Action> getActions() {
        if (actions == null) {
            actions = new ArrayList<Action>();
        }
        return actions;
    }

    public void setActions(ArrayList<Action> actions) {
        this.actions = actions;
    }

    @Override
    public String toString() {
        return "Image{" + "filename=" + filename + "filepath=" + filepath + "width=" + width + "height=" + height + "image=" + image + "x=" + x + "y=" + y + "resize_x=" + resize_x + "resize_y=" + resize_y + '}';
    }

    public String getName() {
        return this.filename;
    }

    public int getEndFrame() {
        return endFrame;
    }

    public void setEndFrame(int endFrame) {
        this.endFrame = endFrame;
    }

    public int getStartFrame() {
        return startFrame;
    }

    public void setStartFrame(int startFrame) {
        this.startFrame = startFrame;
    }

    public void setName(String name) {
        this.filename = name;
    }
}
